import gi , os
# Since a system can have multiple versions 
# of GTK + installed, we want to make  
# sure that we are importing GTK + 3. 
gi.require_version("Gtk", "3.0") 
gi.require_version("Vte", "2.91")
from gi.repository import Gtk , Gdk, Vte, GLib

# import sys
# sys.path.append(".")
import term, tab
  
class TermNotebook(Gtk.Notebook): 
    def __init__(self,window): 
        Gtk.Notebook.__init__(self) 
        self.set_scrollable(True)
        self.window = window
        # Create Terms 
        self.new_term()

        self.connect('show', self.init_focus)
        mbox = self.init_drag_box()
        btn = Gtk.Label(label='{ JingTerm }')
        btn.set_name('tab-drag-btn')
        event_box = Gtk.EventBox()
        event_box.add(mbox)
        event_box.connect("button_press_event",self.on_drag_label)
        event_box.connect('enter_notify_event',self.on_enter_notify_event)
        event_box.connect('leave_notify_event',self.on_leave_notify_event)
        mbox.pack_end(btn,True,True,0)
        self.set_action_widget(event_box, Gtk.PackType.END)
        event_box.show_all()

    def init_drag_box(self):
        mbox = Gtk.Box(spacing=0)
        btn_close = Gtk.ToolButton()
        btn_max = Gtk.ToolButton()
        btn_min = Gtk.ToolButton()
        btn_new = Gtk.ToolButton()
        mbox.pack_end(btn_close,False,False,0)
        mbox.pack_end(btn_max,False,False,0)
        mbox.pack_end(btn_min,False,False,0)
        mbox.pack_end(btn_new,False,False,0)
        icon_close = Gtk.Image()
        icon_close.set_from_file("images/win_close.svg")
        icon_max = Gtk.Image()
        icon_max.set_from_file("images/win_max.svg")
        icon_min = Gtk.Image()
        icon_min.set_from_file("images/win_min.svg")
        icon_new = Gtk.Image()
        icon_new.set_from_file("images/add.svg")
        btn_close.set_icon_widget(icon_close)
        btn_max.set_icon_widget(icon_max)
        btn_min.set_icon_widget(icon_min)
        btn_new.set_icon_widget(icon_new)
        btn_close.connect("clicked", self.window.quitApp)
        btn_max.connect("clicked", self.window.do_max)
        btn_min.connect("clicked", self.window.do_min)
        btn_new.connect("clicked", self.on_add_term)

        # for btn in [btn_close,btn_max,btn_min,btn_new]:
        #     btn.connect("button_press_event",self.on_drag_label)

        return mbox

    def on_enter_notify_event(self,widget, event):
        self.get_window().set_cursor(Gdk.Cursor(Gdk.CursorType.HAND1))
        return True

    def on_leave_notify_event(self,widget, event):
        self.get_window().set_cursor(None)
        return True

    def on_add_term(self,widget):
        self.new_term()

    def on_drag_label(self, widget, event):
        if event.button==Gdk.BUTTON_PRIMARY :
            # print('start drag')
            self.window.begin_move_drag(event.button,event.x_root, event.y_root, event.time)
            return True

    def init_focus(self, widget):
        # 还不行，不能在界面启动后，自动聚焦到第一个终端
        print('init focus')
        self.grab_focus()

    def new_term(self, label=None, callback=None):
        n = self.get_n_pages()
        title = "term#{}".format(n+1)
        if label:
            title = label

        cur_term = self.current_term()
        cwd = None
        if cur_term:
            cwd = cur_term.get_cwd()

        t = term.Term(title, self, init_dir=cwd, callback=callback)
        
        # t.feed_child("ll\r".encode("utf-8"))
        self.insert_page(t, tab.ClosableTab(title, self,t), n) 
        self.set_tab_reorderable(t,True)
        self.show_all()
        self.set_current_page(n)
        t.grab_focus()
        return t

    # 更新窗口透明度，但是最低不得低于0.2，否则就看不清了
    def update_opacity(self,opacity):
        # print('new opacity: ', opacity) #打开这里，方便调试合适的透明度
        if opacity<0.2:
            opacity=0.2
        self.set_opacity(opacity)
        self.opacity = opacity

    #增加透明度
    def increase_opacity(self):
        self.update_opacity(self.opacity-0.1)

    #降低透明度
    def decrease_opacity(self):
        self.update_opacity(self.opacity+0.1)

    def batch_new_term(self, commands):
        # print('batch_new_term:', commands)
        for c in commands:
            self.new_tab(None,c[1:])

    def new_tab(self, label, commands):
        def callback(term):
            term.start_commands_thread(commands)
        self.new_term(label=label, callback=callback)

    def next_term(self):
        self.next_page()

    def previous_term(self):
        self.prev_page()

    def move_to_next(self):
        n = self.get_current_page()
        if n < self.get_n_pages()-1:
            widget = self.get_nth_page(n)
            self.reorder_child(widget,n+1)

    def move_to_previous(self):
        n = self.get_current_page()
        if n>=1 :
            widget = self.get_nth_page(n)
            self.reorder_child(widget,n-1)

    def close_term(self, term):
        n = -1
        if term:
            n = self.page_num(term)
            term.destroy()
            self.remove_page(n)
        else:
            n = self.get_current_page()
            term = self.get_nth_page(n)
            term.destroy()
            self.remove_page(n)
        if self.get_n_pages()==0:
            self.window.quitApp()
        return True


    def current_term(self):
        n = self.get_current_page()
        widget = self.get_nth_page(n)
        return widget

    def focus_term(self):
        widget = self.current_term()
        widget.grab_focus()

    def rename_term(self):
        widget = self.current_term()
        closable_tab = self.get_tab_label(widget)
        label_text = closable_tab.label_text
        dialog = RenameDialog(self.window, label_text)
        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            # print("The OK button was clicked", dialog.get_text())
            self.rename(widget,dialog.get_text(),True) #强制更新标题
        # elif response == Gtk.ResponseType.CANCEL:
            # print("The Cancel button was clicked")

        dialog.destroy()

    # force: 是否强制更新标题，如果设置了，那么以后就固定为本标题，除非通过对话框重新设置
    # 目前只有通过对话框或者通过title:指令来进行设置
    def rename(self,term, text, force=None):
         # 已经强制设置过名称的，不再自动跟随终端标题变化，只能再次强制设置
        if force==None and hasattr(term,'title_changed_by_force') :
            return
        if force:
            term.title_changed_by_force = True
        closable_tab = self.get_tab_label(term)
        closable_tab.set_text(text)

from keymap import *

class RenameDialog(Gtk.Dialog):
    def __init__(self, parent, text):
        Gtk.Dialog.__init__(self, title="重命名", transient_for=parent, flags=0)
        self.add_buttons(
            Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK, Gtk.ResponseType.OK
        )

        self.set_default_size(300, 100)

        entry = Gtk.Entry()
        entry.props.text=text
        # 回车直接关闭窗口
        entry.connect("key-press-event", self.on_key_press_event)
        box = self.get_content_area()
        box.add(entry)
        self.entry = entry
        self.show_all()

    def get_text(self):
        return self.entry.props.text

    def on_key_press_event(self, widget, key_event):
        keyevent_name = get_keyevent_name(key_event)
        if keyevent_name == "Enter":
            self.emit('response',Gtk.ResponseType.OK)
            return True
